#include "lengthUnit.h"
Amount table_length[] = {MILE, YARD, FEET, INCH};
string table_names[] = {"MILE", "YARD", "FEET", "INCH"};

LengthUnit::LengthUnit(Amount a) :
		baseUnit(a) {
}

#define DECL_GET_METHOD(name) \
		LengthUnit& LengthUnit::get##name() {\
			static LengthUnit instance(name);\
			return instance;\
		}

DECL_GET_METHOD(MILE)
DECL_GET_METHOD(YARD)
DECL_GET_METHOD(FEET)
DECL_GET_METHOD(INCH)
DECL_GET_METHOD(BASE)

Amount LengthUnit::BaseUnit() const {
	return baseUnit;
}

string LengthUnit::toStr(Amount val) {
	Amount basetmp = val*baseUnit;
	Amount count = 0;
	string res;
	for (unsigned int i = 0; i < sizeof(table_length)/sizeof(table_length[0]); i++) {
		count = basetmp / table_length[i];
		basetmp = basetmp - count*table_length[i];
		if (count != 0) {
			res.append(amountTostring(count) + " ");
			res.append(table_names[i]);
			if(basetmp!=0)
				res.append(" ");
		}
	}
	return res;
}
